package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.data.OrderInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;
import com.xgkj.diyiketang.http.engine.TaskResultCode;

/**
 * Created by windo on 16/4/20.
 */
public class YueZhiFuTask extends CouponHttpBaseTask{
    /**
     * Constructor of BaseTask, init members and assign task id.
     *objectId
     type
     money
     * @param type task type
     */

    private String mObjId;
    private String mType;
    private String mMoney;
    private String mvipCommUser;

    public YueZhiFuTask(String objectId, String type,String money,String vipCommUser) {
        super(TaskType.YUEZHIFU_TASK);
        mObjId = objectId;
        mType = type;
        mMoney = money;
        mvipCommUser =vipCommUser;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject jsonObject = response.getBody();
        OrderInfo orderInfo = null;
        if (jsonObject != null){
            orderInfo = JSON.parseObject(jsonObject.toString(),OrderInfo.class);
        }
        notifyTaskCompleted(TaskResultCode.SUCCESS,orderInfo);
    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase ==TASK_PHASE_INIT){
            CouponHttpRequest request = new CouponHttpRequest(RequestType.YUEZHIFU_REQUEST);
            JSONObject json = new JSONObject();
            json.put("objectId",mObjId);
            json.put("type",mType);
            json.put("money",mMoney);
            json.put("vipCommUser",mvipCommUser);

            request.setData(json.toString());
            request.setApiNo(ApiNo.YUEZHIFU);
            addRequest(request);
        }
    }
}
